package com.oncliedisk.administrator.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.oncliedisk.administrator.interfaces.onDownFileListener;
import com.oncliedisk.administrator.model.FileTaskModel;
import com.oncliedisk.administrator.utils.LargeFileUpload;
import com.vdisk.android.VDiskAuthSession;
import com.vdisk.net.VDiskAPI;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/21.
 */
public class UploadService extends Service {

    private IBinder binder;
    private List<LargeFileUpload> list = new ArrayList<LargeFileUpload>();
    public static boolean isDowning; //正在下载
    private boolean isTopDown; //是否是第一次要下载文件
    public static final int THEAD_CLOSE = 0;
    public static int THREAD_EXCEL = 1;

    public IBinder onBind(Intent intent) {
        binder = new MyBinder();
        return binder;
    }

    public class MyBinder extends Binder {

        /**
         * 获取Service对象
         *
         * @return
         */
        public UploadService getService() {
            return UploadService.this;
        }
    }

    /**
     * 下载文件
     */
    public void downFile() {
        //如果是第一次把要下载的文件添加到下载列表，就从第一个开始下载。如果是第二次要下载，那就不做操作了，等待其他文件下载成功后，按顺序下载下去
        if (isTopDown == true) {
            //如果是第一次下载，
            Log.i("info","开始下载第一个");
            isTopDown = false;
            LargeFileUpload downFile = list.get(0);
            downFile.execute();
        }
    }

    /**
     * 是要关闭线程还是继续线程
     */
    public void downAction(FileTaskModel molder, int type, Context context, VDiskAPI<VDiskAuthSession> mApi, onDownFileListener onDownFileListener,LargeFileUpload largeFileUpload) {
        LargeFileUpload newTask = null;
        LargeFileUpload oldTask = null;
        //根据对象，开启对象对应的线程进行下载
        for (LargeFileUpload file : list) {
            if (file != null) {
                if (file.model.getId() == molder.getId()) {
                    if (type == THEAD_CLOSE) {
                        file.closeThead();
                        oldTask = file;
                    } else if (type == THREAD_EXCEL) {
                        //如果不关闭，要进行继续,则重新开启一个线程
                        oldTask = null;
                    }
                }
            }
        }
        if (oldTask != null) {
            //如果要
            list.remove(oldTask);
        } else {
            list.add(largeFileUpload);
        }
    }


    /**
     * 下载成功后，默认下载第一个文件
     */
    public void downFileByFinish(FileTaskModel model, Context context, VDiskAPI<VDiskAuthSession> mApi, onDownFileListener onDownFileListener) {
        Log.i("info", "还有需要下载的线程" + list.size() + "个");
        Log.i("info", "开启下载");
        LargeFileUpload files = null;
        for (LargeFileUpload file : list) {
            if (file.model.getId() == model.getId()) {
                files = file;
            }
        }
        list.remove(files);

        //如果还有线程正在下载
        if (list.size() > 0) {
            Log.i("info","还有需要下载的线程"+list.size()+"个");
            LargeFileUpload downFile = list.get(0);
            FileTaskModel model1 = downFile.model;
            list.remove(downFile);
            LargeFileUpload downFile1 = new LargeFileUpload(context, model1, mApi, onDownFileListener);
            list.add(0, downFile);
            downFile1.execute();
        }
    }


    /**
     * 添加要下载的文件对象
     *
     * @param model
     */
    public void addFileModel(Context context, FileTaskModel model, VDiskAPI<VDiskAuthSession> mApi, onDownFileListener onDownFileListener) {
        LargeFileUpload file = new LargeFileUpload(context, model, mApi, onDownFileListener);
        if (!list.contains(file)) {
            if (list.size() == 0) {
                isTopDown = true;
            }
            list.add(file);
            Log.i("info","已添加"+list.size()+"个线程");
        }
    }


    /**
     * 停止线程
     */
    public void stopDownFile(FileTaskModel model) {
        for (LargeFileUpload model1 : list) {
            if (model1.model.getId() == model.getId()) {
                model1.closeThead();
            }
        }
    }


}
